package edu.drexel.jrexel2d.objects2D;

public class Line2D {
	private Point2D p1 ;
	private Point2D p2 ;
	private int x ;		// x and y coords of the center, used to know where the
	private int y ;		// object will lie in relation to other objects
	
	public Line2D(Point2D p1, Point2D p2) {
		this.p1 = p1 ;
		this.p2 = p2 ;
		x = 0 ;
		y = 0 ;
	}
	
	public Line2D(int x1, int y1, int x2, int y2) {
		this.p1 = new Point2D(x1, y1) ;
		this.p2 = new Point2D(x2, y2) ;
		x = 0 ;
		y = 0 ;
	}
	
	public void setCenter(int x, int y) {
		this.x = x ;
		this.y = y ;
	}
	
	public void setCenter(Point2D p) {
		x = p.getX() ;
		y = p.getY() ;
	}
	
	public Point2D getP1() {
		return new Point2D(p1,x,y) ;
	}
	
	public Point2D getP2() {
		return new Point2D(p2,x,y) ;
	}
	
	public boolean check(Line2D l) {
		Point2D r = new Point2D(this.getP1(), this.getP2()) ;
		Point2D s = new Point2D(l.getP1(), l.getP2()) ;
		float rs = r.cross(s) ;
		
		Point2D qp = new Point2D(this.getP1(), l.getP1()) ;
		float qps = qp.cross(s) ;
		float qpr = qp.cross(r) ;
		if(rs == 0) {	// The lines are parrallel
			if(qpr == 0) { // The lines are co-linear
				// I should probably check something here, but I don't think that case will matter much...
				return false ;
			}
			return false ;
		}
		
		float t = qps/rs ;
		float u = qpr/rs ;
		
		return (0<=t && t<=1 && 0<=u && u<=1) ;
	}

}
